package com.atguigu.campusintelligentassistantsystem.controller;

import com.atguigu.campusintelligentassistantsystem.entity.LeaveApply;
import com.atguigu.campusintelligentassistantsystem.service.LeaveApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/leave-apply")
@CrossOrigin(origins = "*")
public class LeaveApplyController {

    @Autowired
    private LeaveApplyService leaveApplyService;

    /**
     * 提交请假申请
     * @param leaveApply 请假信息
     * @return 保存后的请假申请
     */
    @PostMapping
    public ResponseEntity<LeaveApply> submitLeaveApply(@RequestBody LeaveApply leaveApply) {
        LeaveApply savedLeaveApply = leaveApplyService.submitLeaveApply(leaveApply);
        return ResponseEntity.ok(savedLeaveApply);
    }

    /**
     * 根据ID获取请假申请
     * @param id 请假申请ID
     * @return 请假申请信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<LeaveApply> getLeaveApplyById(@PathVariable Long id) {
        LeaveApply leaveApply = leaveApplyService.getLeaveApplyById(id);
        if (leaveApply != null) {
            return ResponseEntity.ok(leaveApply);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 根据学生ID获取请假记录
     * @param studentId 学生ID
     * @return 请假记录列表
     */
    @GetMapping("/student/{studentId}")
    public ResponseEntity<List<LeaveApply>> getLeaveAppliesByStudentId(@PathVariable Long studentId) {
        List<LeaveApply> leaveApplies = leaveApplyService.getLeaveAppliesByStudentId(studentId);
        return ResponseEntity.ok(leaveApplies);
    }

    /**
     * 获取所有请假申请
     * @return 请假申请列表
     */
    @GetMapping
    public ResponseEntity<List<LeaveApply>> getAllLeaveApplies() {
        List<LeaveApply> leaveApplies = leaveApplyService.getAllLeaveApplies();
        return ResponseEntity.ok(leaveApplies);
    }

    /**
     * 更新请假申请状态
     * @param id 请假申请ID
     * @param status 状态
     * @return 更新后的请假申请
     */
    @PutMapping("/{id}/status")
    public ResponseEntity<LeaveApply> updateLeaveApplyStatus(@PathVariable Long id, @RequestParam String status) {
        LeaveApply updatedLeaveApply = leaveApplyService.updateLeaveApplyStatus(id, status);
        if (updatedLeaveApply != null) {
            return ResponseEntity.ok(updatedLeaveApply);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    /**
     * 删除请假申请
     * @param id 请假申请ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<String> deleteLeaveApply(@PathVariable Long id) {
        leaveApplyService.deleteLeaveApply(id);
        return ResponseEntity.ok("请假申请删除成功");
    }
}